<template>
  <div class="login">
    <div class="box">
      <div class="header">
        <img src="@/assets/login_title.png" alt="" />
      </div>
      <div class="body">
        <!-- 表单验证 -->
        <el-form :model="form" :rules="rules" ref="form">
          <el-form-item prop="username">
            <el-input
              placeholder="请输入用户名"
              prefix-icon="el-icon-user"
              v-model="form.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              v-model="form.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              round
              style="width:100%"
              class="login"
              @click="Lgn()"
              :loading="loading"
              >登录</el-button
            >
          </el-form-item>
          <el-form-item>
            <router-link to="/reg" class="go_reg">没有账号?去注册</router-link>
          </el-form-item>
          <!-- {{$store.state.login.token}} -->
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { Login } from '@/api/login'
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '必填', triggle: 'change' },
          {
            validator: (rule, value, callback) => {
              const reg = /^[a-zA-Z0-9]{1,10}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('用户名必须1-10位,数字字母组成'))
              }
            },
            triggle: 'change'
          }
        ],
        password: [
          { required: true, message: '必填', triggle: 'change' },
          {
            validator: (rule, value, callback) => {
              const reg = /^[a-zA-Z0-9]{6,15}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('密码必须6-16位,数字字母组成'))
              }
            },
            triggle: 'change'
          }
        ]
      },
      loading: false
    }
  },
  methods: {
    Lgn () {
      this.$refs.form.validate(result => {
        this.loading = true
        if (result) {
          Login(this.form)
            .then(res => {
              console.log(res);
              this.$message.success(res.message)
              this.$store.commit('login/setToken',res.token)
            })
            .catch(err => {
              console.log(err)
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  height: 100%;
  background: url(~@/assets/login_bg.jpg) center;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 400px;
    background-color: #fff;
    padding: 0 40px;
    .header {
      text-align: center;
      padding: 20px 0;
    }
    .body {
      text-align: center;
      font-size: 12px;
      .login {
        font-size: 12px;
      }
    }
  }
}
</style>
